package com.css.apps.base.common.upload;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import com.css.core.model.IServiceResult;
import com.css.util.Ajax;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.Action;

/**
 * <action name="downloadTemplateFile" class=
 * "com.css.apps.information.template.action.DownloadTemplateFile" caption=
 * "上传模板文件"> <result name="success" type="stream">
 * <param name="contentType">application/octet-stream</param>
 * <param name="inputName">inputStream</param> <param name=
 * "contentDisposition">attachment;filename="${downloadFileName}"</param>
 * <param name="bufferSize">4096</param> </result>
 * <result name="error" type="dispatcher">
 * <param name="location">doError.action</param> </result> </action>
 * 
 * @author FangWQ
 * @version v0.1
 * @since 2014-5-26 下午04:49:12
 */
@SuppressWarnings("serial")
public class DownloadFile extends AdminAction {
	
	public String uuid;
	public String fileName;
	public String downloadFileName;

	/**
	 * 设置当前下载文件的绝对路径
	 * 
	 * @override应该被重写
	 * @throws Exception
	 */
	protected void setFileName() throws Exception {
	}

	@Override
	protected String adminGo() {
		try {
			setFileName();
		} catch (Exception e) {
			String msg = e.getMessage();
			// log.error(msg,e);
			result = Ajax.xmlResult(IServiceResult.RESULT_ERROR, msg);
			fileName = "文件不存在.txt";
			downloadFileName = "文件不存在";
			return Action.SUCCESS;
		}
		return Action.SUCCESS;
	}

	/**
	 * 获取文件流
	 * 
	 * @return
	 * @throws Exception
	 */
	public InputStream getInputStream() throws UnsupportedEncodingException {
		InputStream is = null;
		try {
			is = new OnceFileInputStream(fileName);
		} catch (FileNotFoundException e) {
			is = new ByteArrayInputStream(new byte[] {});
			downloadFileName = "文件不存在";
		}
		return is;
	}

	public void setDownloadFileName(String downloadFileName) {
		this.downloadFileName = downloadFileName;
	}

	/**
	 * 获取文件名称
	 * 
	 * @return
	 */
	public String getDownloadFileName() {
		String tmpName = downloadFileName;
		if (StringHelper.isEmpty(tmpName)) {
			tmpName = AttachItem.getFileFullName(fileName);
		} else {
			tmpName += "." + AttachItem.getExtension(fileName);
		}
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			if (StringHelper.isNotEmpty(request.getHeader("User-Agent"))) {
				if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0)
					tmpName = new String(tmpName.getBytes("UTF-8"), "ISO8859-1");// firefox浏览器
				else if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0)
					tmpName = URLEncoder.encode(tmpName, "UTF-8");// IE浏览器
				else if (request.getHeader("User-Agent").toUpperCase().indexOf("Mozilla") > 0)
					tmpName = new String(tmpName.getBytes("UTF-8"), "ISO8859-1");// chrome
				else
					tmpName = URLEncoder.encode(tmpName, "UTF-8");// other
			}
		} catch (UnsupportedEncodingException e) {
			// log.error(e);
		}
		return tmpName;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

}
